<?php

/**
 *  后台继承类
 * @file   Admin.php  
 * @date   2016-8-23 19:45:21 
 * @author Zhenxun Du<5552123@qq.com>  
 * @version    SVN:$Id:$ 
 */

namespace app\index\controller;
use app\admin\model\Sampling as SamplingModel;
use think\Config;


class Sampling extends Common {


    //pc获取断面列表
    public function index(){
        $post = $this->postData();

        $where = [];
        if(isset($post['posname']) && $post['posname']){
            $where['B.posname'] = ['like',"%{$post['posname']}%"];
        }
        if(isset($post['com_id']) && $post['com_id']){
            $where['A.com_id'] = $post['com_id'];
        }
        $field = "A.*,B.name as company_name,C.posname";
        $lists = db("sampling")->alias("A")->field($field)
            ->join("cy_company B","A.com_id=B.id","left")
            ->join("cy_section C","A.section_id=C.id","left")
            ->where($where)->order("A.id desc")->paginate(20);




        $samplingType = Config::get('sampling_type');
        $this->assign('samplingType', $samplingType);

        $company = db('company')->select();
        $this->assign('company', $company);

        $this->assign('lists', $lists);
        $this->assign('posname', $post['posname']);
        $this->assign('com_id', $post['com_id']);

        return $this->fetch();
    }

    public function info() {

        $id = input('id');

        $company = db('company')->select();

        $province = db('region')->where(['parent_id'=>0])->select();
        if ($id) {
            //当前用户信息
            $info = db('sampling')->where(['id'=>$id])->find();
            $this->assign('info', $info);

        }

        $taskType = Config::get('task_type');
        $this->assign('taskType', $taskType);

        $samplingType = Config::get('sampling_type');
        $this->assign('samplingType', $samplingType);
        $this->assign('company', $company);
        $this->assign('province', $province);
        $this->assign('id', $id);

        return $this->fetch();
    }


    public function add(){
        $post = $this->postData();
        if(!isset($post['sampleno']) || !$post['sampleno']){
            return  $this->renderError('样品编号不能为空');
        }
        $samplingModel = new SamplingModel();
        //判断采样编号是否已存在
        $where['sampleno'] = trim($post['sampleno']);
        $samArr = $samplingModel->getSampling($where);
        if($samArr){
            return  $this->renderError('采样编号已存在');
        }
        if(!isset($post['com_id']) || !$post['com_id']){
            return  $this->renderError('集团id不能为空');
        }
        if(!isset($post['section_id']) || !$post['section_id']){
            return  $this->renderError('断面不能为空');
        }

        if(!isset($post['sample_type']) || !$post['sample_type']){
            return  $this->renderError('样品类型不能为空');
        }

        if(!isset($post['preservative_content']) || !$post['preservative_content']){
            return  $this->renderError('保存剂不能为空');
        }
        if(!isset($post['sample_time']) || !$post['sample_time']){
            return  $this->renderError('采样时间不能为空');
        }
        if(!isset($post['sample_person']) || !$post['sample_person']){
            return  $this->renderError('采样人不能为空');
        }
        $samplePersonArr = explode(',',$post['sample_person']);
        if(count($samplePersonArr)<2 || !$samplePersonArr[0] || !$samplePersonArr[1]){
            return  $this->renderError('采样人必须是2个人');
        }

        $post['com_id'] = $post['com_id'];
        $post['sample_time'] = strtotime($post['sample_time']);
        $post['sample_uid'] = 0;
        $post['create_time'] = time();
        $post['sampleqty'] = 0;
        if($post['preservative_content']){
            $post['preservative_content'] = str_replace('&quot;', '"', $post['preservative_content']);
            $preArr = json_decode($post['preservative_content'],true);

            $preNum = 0;
            foreach($preArr as $k=>$v){
                $preNum+= count(explode(',',$v['value']));
            }
            $post['sampleqty'] = $preNum;
        }



        if($samplingModel->insert($post)!==false){
            return $this->renderSuccess();
        }else{
            return  $this->renderError('添加失败');
        }
    }

    //pc断面添加
    public function edit(){
        $post = $this->postData();
        if(!isset($post['id']) || !$post['id']){
            return  $this->renderError('id不能为空');
        }
        if(!isset($post['sampleno']) || !$post['sampleno']){
            return  $this->renderError('样品编号不能为空');
        }
        $samplingModel = new SamplingModel();
        //判断采样编号是否已存在
        $where['sampleno'] = trim($post['sampleno']);
        $where['id'] = ['neq',$post['id']];
        $samArr = $samplingModel->getSampling($where,$post['id']);
        if($samArr){
            return  $this->renderError('采样编号已存在');
        }

        if(!isset($post['com_id']) || !$post['com_id']){
            return  $this->renderError('集团id不能为空');
        }
        if(!isset($post['section_id']) || !$post['section_id']){
            return  $this->renderError('断面不能为空');
        }

        if(!isset($post['sample_type']) || !$post['sample_type']){
            return  $this->renderError('样品类型不能为空');
        }

        if(!isset($post['preservative_content']) || !$post['preservative_content']){
            return  $this->renderError('保存剂不能为空');
        }
        if(!isset($post['sample_time']) || !$post['sample_time']){
            return  $this->renderError('采样时间不能为空');
        }
        if(!isset($post['sample_person']) || !$post['sample_person']){
            return  $this->renderError('采样人不能为空');
        }
        $samplePersonArr = explode(',',$post['sample_person']);
        if(count($samplePersonArr)<2 || !$samplePersonArr[0] || !$samplePersonArr[1]){
            return  $this->renderError('采样人必须是2个人');
        }

        $post['com_id'] = $post['com_id'];
        $post['sample_time'] = strtotime($post['sample_time']);
        $post['sampleqty'] = 0;
        if($post['preservative_content']){
            $post['preservative_content'] = str_replace('&quot;', '"', $post['preservative_content']);
            $preArr = json_decode($post['preservative_content'],true);

            $preNum = 0;
            foreach($preArr as $k=>$v){
                $preNum+= count(explode(',',$v['value']));
            }
            $post['sampleqty'] = $preNum;
        }
        if($samplingModel->where(['id'=>$post['id']])->update($post)!==false){
            return $this->renderSuccess();
        }else{
            return  $this->renderError('编辑失败');
        }

    }

    //删除断面
    public function del(){
        $post = $this->postData();
        if(!isset($post['id'])  || !$post['id']){
            return  $this->renderError('采样id不能为空');
        }
        $samplingModel = new SamplingModel();
        if($samplingModel->where(['id'=>$post['id']])->delete()!==false){
            return $this->renderSuccess();
        }else{
            return  $this->renderError('删除失败');
        }
    }

}
